package utils

import (
	"context"
	"mime/multipart"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// https://github.com/aliyun/alibabacloud-oss-go-sdk-v2/blob/master/README-CN.md

// UploadFileToOss 上传文件到阿里云OSS
func UploadFileToOss(fileName string, file *multipart.File, region, bucketName, accessKeyId, accessKeySecret string) error {
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")).
		WithRegion(region)

	client := oss.NewClient(cfg)

	_, err := client.PutObject(context.Background(), &oss.PutObjectRequest{
		Bucket: oss.Ptr(bucketName),
		Key:    oss.Ptr(fileName),
		Body:   *file,
	})
	
	return err
}
